
//============================================================================
// Copyright (C) 2009-2013 Brett R. Jones 
// All Rights Reserved
//
// You may redistribute and/or modify for non commercial and commercial uses 
// provided this copyright notice remains in place and is not modified
//
// This code is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// brettjones1900@gmail.com
// http://www.p2panarchy.com
//============================================================================

#include "RcSysSktMgrLib.h"


VxTimer g_oProcessFrameTimer;


//! process socket and other events. return true if anything was processed
BOOL RcSysProcessEvents( void )
{

	//BOOL bProcessedEvents = g_oSktMgr.ProcessRxPktAnnQue();
	//bProcessedEvents |= g_oSktMgr.ProcessRxSearchEventQue();
	//bProcessedEvents |= g_oSktMgr.ProcessRxEventQue();
	//// we are shooting for roughly 30 frames per second (33 milliseconds per frame )
	////log_msg( 0, "ProcessEventsTimer %2.3f ms\n", dElapsedMilliSec );
	////double dElapsedMilliSec = g_oProcessFrameTimer.ElapsedMilliSec();
	////if( dElapsedMilliSec > 15.0 ) 
	////{
	////	g_oProcessFrameTimer.StartMilliTimer(0);
	////	g_oPluginMgr.OncePerFrame();
	////	if( dElapsedMilliSec > 50.0 )
	////	{
	////		log_msg( 0, "ProcessEventsTimer Hic up %2.3f ms\n", dElapsedMilliSec );
	////	}
	////	bProcessedEvents = true;
	////}
	//long s32MilliSec = g_oWxProcessFrameTimer.Time();
	////log_msg( 0, "ProcessEventsTimer %d ms\n", s32MilliSec );
	//if( s32MilliSec >= 15 ) 
	//{
	//	g_oWxProcessFrameTimer.Start(0);
	//	g_oPluginMgr.OncePerFrame();
	//	if( s32MilliSec > 50 )
	//	{
	//		//log_msg( 0, "ProcessEventsTimer Hic up %d ms\n", s32MilliSec );
	//	}
	//	bProcessedEvents = true;
	//}

	//return bProcessedEvents;
	return 0;
}
//============================================================================
//! process events in rxed pkt ann que.. return true if any packets processed
BOOL RcSysSktMgr::ProcessRxPktAnnQue( void )
{
	//BOOL bHadEvents = false;
	//RcSysRxPktAnnEvent * poEvent;
	//m_oRxPktAnnEventMutex.Lock();
	//while( m_aoRxPktAnnEvents.size() )
	//{
	//	poEvent = RemoveRxPktAnnEvent();
	//	m_oRxPktAnnEventMutex.Unlock();
	//	if( poEvent )
	//	{
	//		bHadEvents = true;
	//		g_oBigListMgr.Update( poEvent->m_poPktAnn, 0, poEvent->m_u32BigListFlags );
	//		delete poEvent;
	//	}
	//	else
	//	{
	//		m_oRxPktAnnEventMutex.Lock();
	//		break;
	//	}
	//	// unlock then lock so other threads have chance
	//	m_oRxPktAnnEventMutex.Lock();
	//}
	//m_oRxPktAnnEventMutex.Unlock();
	return 0;
}
//============================================================================
//! process events in rxed seach event que.. return true if any packets processed
BOOL RcSysSktMgr::ProcessRxSearchEventQue( void )
{
	//BOOL bHadEvents = false;
	//RcSysRxSearchEvent * poEvent;
	//m_oRxSearchEventMutex.Lock();
	//poEvent = RemoveRxSearchEvent();
	//if( poEvent )
	//{
	//	bHadEvents = true;
	//	g_oPluginMgr.RcFoundUser(	poEvent->m_strPluginName.c_str(), 
	//								poEvent->m_poPktAnn, 
	//								poEvent->m_u64UserData,
	//								poEvent->m_u64SeachHandle );
	//	delete poEvent;
	//}
	//m_oRxSearchEventMutex.Unlock();
	return 0;
}

//============================================================================
//! process events in rx que.. return true if any packets processed
BOOL RcSysSktMgr::ProcessRxEventQue( void )
{
//	BOOL bHadEvents = false;
//	RcSysRxEvent * poEvent;
//	m_oRxEventMutex.Lock();
////	while( m_aoRxEvents.size() )
//	if( m_aoRxEvents.size() )
//	{
//		poEvent = RemoveSktRxEvent();
//		if( NULL == poEvent )
//		{
//			m_oRxEventMutex.Unlock();
//			return bHadEvents;
//		}
//		bHadEvents = true;
//		m_oRxEventMutex.Unlock();
//		if( eSktCallbackReasonData == poEvent->m_eCallbackReason )
//		{
//			if( poEvent->m_poSkt->IsUdpSocket()) 
//			{
//				// do announcement
//				OnPktAnnounce( poEvent->m_poSkt, poEvent->m_poPkt );
//			}
//			else
//			{
//				// tcp 
//				RcSktBase * poSkt		= poEvent->m_poSkt;
//				RcSysPktHdr * poPktHdr	= poEvent->m_poPkt;
//				U16 u16PktType			= poPktHdr->m_u16PktType;
//				U16 u16PktLen			= poPktHdr->m_u16PktLen;
//
//				//pkt offset 0-0x00ff reserved to test for invalid packets
//				//pkt offset 0x0100-0x02ff reserved for Tronacom system packets
//				vx_assert( u16PktType > 0xff );
//				if( TC_SYS_PKT_SERVICE_BIT == (u16PktType & 0xff00 ))
//				{
//					//system packet
//					if( (u16PktType & 0x00ff ) > MAX_SERVICE_PKT_TYPE_CNT )
//					{
//						// invalid system packet type
//						log_msg( 0, "AppRcpCallback.. Skt %d num %d Invalid Tronacom Packet type 0x%x of len  %d\n", 
//								poSkt->GetSocketId(),
//								poSkt->m_iSktId,
//								u16PktType,
//								u16PktLen );
//
//						HackerOffense( 0, 2, poSkt->GetRemoteIpBinary(), "Hacker invalid system pkt from ip %s\n", poSkt->GetRemoteIp() );
//						poSkt->SktBufAmountRead( 0 );
//						poSkt->CloseSkt( 641 );
//						return bHadEvents;
//					}
//					//log_msg( 0, "AppRcpCallback.. Skt %d num %d Handling System Packet Len  %d\n", 
//					//		poSkt->GetSocketId(),
//					//		poSkt->m_iSktId,
//					//		u16PktLen );
//					// jump to packet function handler
//					U16 u16ThisPktType = (u16PktType & 0xff );
//					if(  u16ThisPktType >= MAX_SERVICE_PKT_TYPE_CNT )
//					{
//						HackerOffense( 0, 2, poSkt->GetRemoteIpBinary(), "Hacker invalid service pkt from ip %s\n", poSkt->GetRemoteIp() );
//						poSkt->SktBufAmountRead( 0 );
//						poSkt->CloseSkt( 642 );
//						return bHadEvents;
//					}
//					RcSysHandlePkt( poSkt, poPktHdr );
//
//				}
//				else if( TC_SYS_PKT_PROXY_BIT == (u16PktType & 0xff00 ))
//				{
//					// proxy packet
//					// not implemented yet
//					//log_msg( 0, "AppRcpCallback.. Skt %d num %d Handling Proxy Packet Len %d\n", 
//					//		poSkt->GetSocketId(),
//					//		poSkt->m_iSktId,
//					//		u16PktLen );
//					poSkt->SktBufAmountRead( 0 );
//					poSkt->CloseSkt( 627 );
//					return bHadEvents;
//
//				}
//				else if( u16PktType & 0x3f00 )
//				{
//					//plugin packet
//					//log_msg( 0, "AppRcpCallback.. Skt %d num %d Handling Plugin Packet Len %d\n", 
//					//		poSkt->GetSocketId(),
//					//		poSkt->m_iSktId,
//					//		u16PktLen );
//					g_oPluginMgr.RcHandlePkts(	poSkt,	//socket data is from
//												poPktHdr );	//pointer to data
//				}
//				else
//				{
//					// invalid packet type
//					HackerOffense( 0, 2, poSkt->GetRemoteIpBinary(), "Hacker invalid plugin pkt from ip %s\n", poSkt->GetRemoteIp() );
//					poSkt->SktBufAmountRead( 0 );
//					poSkt->CloseSkt( 628 );
//					return bHadEvents;
//
//				}
//			}
//		}
//		else if( eSktCallbackReasonClosed == poEvent->m_eCallbackReason )
//		{
//			// socket closed
//			g_oPluginMgr.RcSktClosed( poEvent->m_poSkt, poEvent->m_rcError );
//		}
//	}
//	else
//	{
//		m_oRxEventMutex.Unlock();
//	}
//	return bHadEvents;
	return 0;
}